package com.coincalf.wallet.blockchain.client;

import com.coincalf.feign.CoinServiceClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import wf.bitcoin.javabitcoindrpcclient.BitcoinJSONRPCClient;

import javax.annotation.PostConstruct;
import java.net.URL;

/**
 * 获取LTC JAVA客户端
 */
@Slf4j
@Component
public class LtcClientSingleton {

    @Autowired
    private CoinServiceClient coinServiceClient;

    private static CoinServiceClient coinClient;

    @PostConstruct
    public void init() {
        LtcClientSingleton.coinClient = coinServiceClient;
    }

    private LtcClientSingleton() {
    }

    private static volatile BitcoinJSONRPCClient ltcClient = null;

    public static BitcoinJSONRPCClient getLtcClient() {
        if (ltcClient == null) {
            synchronized (LtcClientSingleton.class) {
                if (ltcClient == null) {
                    try {
                        ltcClient = new BitcoinJSONRPCClient(new URL(coinClient.selectRpcServerUrl("LTC")));
                    } catch (Exception e) {
                        log.warn("获取LTC CLIENT ERROR e={}", e);
                        e.printStackTrace();
                    }
                }
            }
        }
        return ltcClient;
    }

}
